using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class Utils : MonoBehaviour
{
	public delegate void SessionEvent();

	private static string storePageURL;

	private static string storePageURLRate;

	private static string facebookPageURL;

	private static string facebookAppURL;

	private static string twitterPageURL;

	private static string twitterAppURL;

	private DateTime pauseTime;

	private bool leftApp;

	private bool leftApp2;

	private bool facebookPageOppening;

	private bool twitterOppening;

	private bool started;

	public Button shareButton;

	public Button twitterButton;

	public Button facebookButton;

	public ShareableSkinItem fbItem;

	public ShareableSkinItem twItem;

	public static Utils instance;

	public static string StorePageURL
	{
		get
		{
			return storePageURL;
		}
	}

	public static string StorePageURLRate
	{
		get
		{
			return storePageURLRate;
		}
	}

	public static string FacebookPageURL
	{
		get
		{
			return facebookPageURL;
		}
	}

	public static string FacebookAppURL
	{
		get
		{
			return facebookAppURL;
		}
	}

	public static string TwitterPageURL
	{
		get
		{
			return twitterPageURL;
		}
	}

	public static string TwitterAppURL
	{
		get
		{
			return twitterAppURL;
		}
	}

	public int SessionCount
	{
		get
		{
			return PlayerPrefs.GetInt("AppSessionCount", 0);
		}
		private set
		{
			PlayerPrefs.SetInt("AppSessionCount", value);
			if (Utils.OnSessionCountChanged != null)
			{
				Utils.OnSessionCountChanged();
			}
		}
	}

	public static event SessionEvent OnSessionCountChanged;

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
		}
		else
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
	}

	private void Start()
	{
		facebookPageURL = "https://www.facebook.com/fispiogame/";
		twitterPageURL = "https://www.twitter.com/fispiogame/";
		storePageURL = "http://play.google.com/store/apps/details?id=" + Application.identifier;
		storePageURLRate = "market://details?id=" + Application.identifier;
		facebookAppURL = "fb://page/1289084724538057";
		twitterAppURL = "twitter://user?screen_name=fispiogame";
		started = true;
		SessionCount++;
		shareButton.onClick.AddListener(ShareGame);
		facebookButton.onClick.AddListener(OpenFacebookPage);
		twitterButton.onClick.AddListener(OpenTwitterPage);
	}

	private void OnApplicationPause(bool paused)
	{
		if (paused)
		{
			pauseTime = DateTime.Now;
			leftApp = true;
			leftApp2 = true;
		}
		else if (started && (DateTime.Now - pauseTime).TotalSeconds >= 60.0)
		{
			SessionCount++;
		}
	}

	public void ShareGame()
	{
		AnalyticManager.SendEvent(AnalyticManager.ShareGame);
		//UM_ShareUtility.ShareMedia("Share...", "Best #fidgetspinner game on the market! Join #fispio " + StorePageURL);
	}

	public void OpenFacebookPage()
	{
		if (!facebookPageOppening)
		{
			StartCoroutine(OpenFacebookPageRoutine());
		}
	}

	public void OpenTwitterPage()
	{
		if (!twitterOppening)
		{
			StartCoroutine(OpenTwitterPageRoutine());
		}
	}

	private IEnumerator OpenFacebookPageRoutine()
	{
		facebookPageOppening = true;
		leftApp = false;
		AnalyticManager.SendEvent(AnalyticManager.OpenFacebookPage);
		Application.OpenURL(facebookAppURL);
		yield return new WaitForSeconds(0.75f);
		if (leftApp)
		{
			leftApp = false;
		}
		else
		{
			Application.OpenURL(facebookPageURL);
			yield return new WaitForSeconds(0.1f);
		}
		twitterButton.gameObject.SetActive(true);
		facebookButton.gameObject.SetActive(false);
		facebookPageOppening = false;
		if (!fbItem.Opened)
		{
			yield return new WaitForSeconds(0.1f);
			fbItem.OpenItem();
			UIScreenController.instance.OpenCongrats();
		}
	}

	private IEnumerator OpenTwitterPageRoutine()
	{
		twitterOppening = true;
		leftApp2 = false;
		AnalyticManager.SendEvent(AnalyticManager.OpenTwitterPage);
		Application.OpenURL(twitterAppURL);
		yield return new WaitForSeconds(0.75f);
		if (leftApp2)
		{
			leftApp2 = false;
		}
		else
		{
			Application.OpenURL(twitterPageURL);
			yield return new WaitForSeconds(0.1f);
		}
		facebookButton.gameObject.SetActive(true);
		twitterButton.gameObject.SetActive(false);
		twitterOppening = false;
		if (!twItem.Opened)
		{
			yield return new WaitForSeconds(0.1f);
			twItem.OpenItem();
			UIScreenController.instance.OpenCongrats();
		}
	}

	public void OpenRatePage()
	{
		AnalyticManager.SendEvent(AnalyticManager.OpenRatePage);
		Application.OpenURL(storePageURLRate);
	}
}
